package com.um.jdy.platform.member.manager;

import com.um.jdy.common.commons.utils.StpKit;
import com.um.jdy.common.member.code.UserCode;
import com.um.jdy.common.member.entity.po.MemberUser;
import com.um.jdy.common.member.entity.vo.MemberUserVO;
import com.um.jdy.common.member.enums.MemberEnum;
import com.um.jdy.common.member.service.MemberUserService;
import com.um.springboot.starter.entity.Page;
import com.um.springboot.starter.orm.entity.Response;
import com.um.springboot.starter.orm.manager.BaseManager;
import com.um.springboot.starter.orm.params.PageParams;
import com.um.springboot.starter.utils.UMBeanUtils;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MemberUserManager extends BaseManager<MemberUserService> {

    /**
     * 分页查询订单
     * @param pageParams
     * @return
     */
    public Response<Page<MemberUserVO>> page(PageParams pageParams) {
        Page<MemberUserVO> page = new Page<>();
        page.setPageNo(pageParams.getPageNo());
        page.setPageSize(pageParams.getPageSize());
        pageParams.getCondition().put("u.com_id",getComId());
        pageParams.getCondition().put("u.is_delete", "0");
        page = service.getCMSViewModelPageBySqlName(page, "get_member_user_page", "get_member_user_count",
                MemberUserVO.class, pageParams.getProperties(), pageParams.getValues(),
                pageParams.getRangeEntitys(), pageParams.getOrderNames(), pageParams.getSortOns());
        return Response.success(page);
    }

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    public Response<MemberUserVO> get(String id) {
        MemberUser entity = service.getEntity(id);
        MemberUserVO vo = UMBeanUtils.copyBean(entity, MemberUserVO.class);
        return Response.success(vo);
    }

    /**
     * 按手机号码精准查询用户列表
     * @param mobile
     * @return
     */
    public Response<List<MemberUserVO>> getListByMobile(String mobile) {
        List<MemberUser> list = service.getList("mobile",mobile);

        return Response.success(UMBeanUtils.copyBeanList(list,MemberUserVO.class));
    }

    /**
     * 修改会员状态
     * @param id
     * @param status
     * @return
     */
    public Response changeStatus(String id,String status){
        MemberUser memberUser = service.getEntity(id);
        if(memberUser == null){
            Response.failed(UserCode.NoUser);
        }
        memberUser.setMemberUserId(id);
        memberUser.setStatus(status);
        boolean result = service.update(memberUser);
        if(result && MemberEnum.MemberStatus.Disabled.name().equals(status)){
            StpKit.MEMBER.logout(id);
        }
        return result ? Response.success() : Response.failed();
    }
}
